<template>
    <div class="login_container">
      <div class="login_box">
        <!-- 头像区域 -->
        <div class="avatar_box">
          <img src="../assets/start.jpg">
        </div>
        <!-- 表单区域 -->
        <el-form ref="loginFormRef" :model="loginForm" :rules="rules" label-width="0px" class="login_form">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="iconfont icon-touxiang"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" prefix-icon="iconfont icon-mima"></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetLoginForm">重置</el-button>
          </el-form-item>
          <el-form-item class="btn2">
            <el-button type="text" @click="regist">注册</el-button>
            <el-button type="text" disabled style="margin-left: 0">/登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
</template>

<script>
export default {
  data () {
    var checkUserUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'))
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
        console.log(reg.test(value))
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的手机号'))
        }
      }
    }
    return {
      loginForm: {
        username: '15069125317',
        password: '123456'
      },
      // 表单的验证规则对象
      rules: {
        username: [
          { validator: checkUserUsername, trigger: 'blur' },
          { min: 11, max: 11, message: '长度为11个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入正确的密码', trigger: 'blur' },
          { min: 6, max: 11, message: '长度为6-11个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置form表单
    resetLoginForm: function () {
      // console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(
        async valid => {
          if (!valid) return
          const { data: res } = await this.$http.post('/user/login', this.loginForm)
          if (res.status !== 200) return this.$message.error('登录失败')
          this.$message.success('登录成功')
          // 1.将登录成功之后的token，保存到客户端的sessionStorage中(localStorage是持久化的存储sessionStorage只在会话期间有效)
          //  1.1项目中除了登录之外的其他API接口，必须在登录之后才能访问
          //  1.2token值应在当前网站打开期间生效，所以将token保存在sessionStorage中
          // 2.通过编程式导航跳转到后台主页，路由地址是/home
          window.sessionStorage.setItem('token', res.data.token)
          this.$router.push('/home')
        })
    }
  }
}
</script>
<!--scoped样式只在本组件显示-->
<style lang="less" scoped>
.login_container{
  height: 100%;
  background-color: #2b4b6b;
}
.login_box{
  width: 450px;
  height: 300px;
  background-color: #ffffff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);

  .avatar_box{
    width: 130px;
    height: 130px;
    border: 1px solid #eeeeee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #ffffff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eeeeee;
    }
  }
}
.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns{
  display: flex;
  justify-content: flex-end;
  margin-bottom: 0px;
}
.btn2{
  display: flex;
  justify-content: center;
  margin-bottom: 0px;
}
</style>
